<template>
	<view>
		<view>
			 <view>
				<view class='header'>
					<image src='../../static/wx_login.png'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
					<text>获得你微信绑定的手机号</text>
				</view>
				<button class='bottom' type='primary' open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber">
					手机号登录
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {wxlogin, getPhone} from "@/store/wxlogin.js"
	import {htxcx} from "@/store/api.js"
	const app = getApp();
	export default {
		data() {
			return {
				type:0,
				phone:"",
				phoneParams:{ 
					sessionkey:"",
					ivdata:"",
					encrypdata:""
				},
				phoneStatus:false,
				username:uni.getStorageSync('wxUserName')!=null?uni.getStorageSync('wxUserName'):'',
				gender:uni.getStorageSync('wxGender'),
				headImg:uni.getStorageSync('wxHeadImg')!=null?uni.getStorageSync('wxHeadImg'):null,
			}
		},
		onLoad(option) {
			try{
				this.init()
			}catch(e){
				console.log("init错误信息：",e)
			}
		},
		methods: {
			async init(){
				var code = await wxlogin()
				// 获取sessionkey
				var key=await htxcx(code)
				this.phoneParams.sessionkey = key.data.data.session_key;
			},
			async getPhoneNumber(e){
				this.phoneParams.encrypdata = e.detail.encryptedData
				this.phoneParams.ivdata = e.detail.iv
				var phone = await getPhone(this.phoneParams)
				this.phone = phone.purePhoneNumber
				if(this.phone){
					this.phoneStatus = true

					this.reLaunch()
				}
			},
			reLaunch(){

				if(this.phoneStatus){
					// uni.setStorageSync("tongfang-phone",this.phone);
					uni.request({
						url: app.globalData.serve_system_url + 'wxphoneLogin',
						method: 'GET',
						data: {
							phone:this.phone,
							wxpicImg:this.headImg,
							wxName:this.username,
							gender:this.gender,
						},
						success: res => {
							let dataInfo = res.data.data;
							// uni.setStorageSync('datas',this.$data);
							// uni.setStorageSync('wxHeadImg',this.headImg);
							if(dataInfo.xsjyStudent != null){
								app.globalData.userinfo.studentId = dataInfo.xsjyStudent.studentId;
							}else{
								app.globalData.userinfo.userId = dataInfo.dataSfz.sfzId;
							}
							app.globalData.userinfo.avatar = this.headImg;
							app.globalData.userinfo.user_nickname = this.username;
							app.globalData.userinfo.user_phoneNum = this.phone;
							if(dataInfo.xsjyLianluoyuan != null){
								app.globalData.userinfo.llyId = dataInfo.xsjyLianluoyuan.lianluoyuanId;
							}
							uni.setStorageSync(
								'userinfo',
								app.globalData.userinfo
							);
							uni.navigateBack({
								delta:2
							})																	
						},
						fail(err) {
							console.log(err)
						}
					})
				}
			},
			bindPickerMPChange(e){
				this.type =  e.target.value
			}	
		}
	}
</script>

<style>
	.header {
	    margin: 90rpx 0 50rpx 50rpx;
	    border-bottom: 1px solid #ccc;
	    text-align: center;
	    width: 650rpx;
	    height: 300rpx;
	    line-height: 450rpx;
	}
	.header image {
	    width: 200rpx;
	    height: 200rpx;
	}
	.content {
	    margin-left: 50rpx;
	    margin-bottom: 50rpx;
	}
	.content text {
	    display: block;
	    color: #9d9d9d;
	    margin-top: 40rpx;
	}
	.bottom {
	    border-radius: 10rpx;
	    margin: 35rpx 50rpx;
	    font-size: 35rpx;
	}
	.bottom:first-child{
		margin-top: 50rpx;
	}
</style>